package com.fifth.apartment.service.impl;

import com.fifth.apartment.entity.Orders;
import com.fifth.apartment.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

/**
 * TODO
 *
 * @author Dao
 * @since 2024/7/12
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    public static final String OID = "oid::";
    @Autowired
    //private RedisTemplate<Object,Object> redisTemplate;
    private RedisTemplate redisTemplate;


    @Override
    public Orders add() {
        //随机生成了一个订单号
        int oid = ThreadLocalRandom.current().nextInt(100);
        //随机生成一个流水号
        String serial = UUID.randomUUID().toString();
        Orders orders = new Orders(oid , "流水号："+serial);
        log.info("订单编号：{} ,  , 订单流水号：{}" , oid , serial);
        //在Redis中存储订单数据
        redisTemplate.opsForValue().set(OID+oid , orders);
        return orders;
    }

    @Override
    public Orders find(int id) {
        //在Redis中获取订单数据
        Orders o = (Orders) redisTemplate.opsForValue().get(OID + id);
        log.info("根据订单编号：{} ， 查询出订单信息：{}" , id , o);
        return o;
    }
}
